package com.example.monthmoni.security;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author 宋伟宁
 * @Date 2023/11/28
 * @Version 1.0
 **/
@Component
public class AuthFaileHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
          response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
          response.setContentType("application/json;charset=utf-8");
          PrintWriter out = response.getWriter();
          out.write("{\"code\":\"401\",\"msg\":\"登录失败\"}");
          out.flush();
          out.close();
    }
}
